import Axios from "axios";
const Base = import.meta.env.VITE_AXIOS_BASE_URL
const baseURL = import.meta.env.VITE_AXIOS_BASE_URL
import RequestInterceptor from "./interceptors/request";
import ResponseInterceptor from "./interceptors/response";
let instance = Axios.create({baseURL});
instance.interceptors.request.use(
  RequestInterceptor
);
instance.interceptors.response.use(
  (res) => {
    if (res.data.code == 0) {
      return res.data;
    } else {
      return Promise.reject(res.data);
    }
  }, (err) => {
    let message = err.message;
    let code = err.code;
    if (message == "Network Error") {
      return Promise.reject({
        code: 500,
        msg: "服务器无法访问",
      });
    } else if (code == "ECONNABORTED") {
      return Promise.reject({
        code: 500,
        msg: "请求超时",
      });
    } else if (message == "Request failed with status code 404") {
      return Promise.reject({
        code: 404,
        msg: "接口不存在",
      });
    } else {
      return Promise.reject({
        code: 5003,
        msg: "接口请求出错,未知错误",
      });
    }
  }
)
export default instance;